//
//  ZHModel.m
//  PiankeTest
//
//  Created by 邹浩 on 15/12/11.
//  Copyright © 2015年 蓝鸥. All rights reserved.
//

#import "NSObject+ZHModel.h"
#import <UIKit/UIKit.h>

@implementation NSObject(ZHModel)

//字典直接转模型
+ (id)modelWithDictionary:(NSDictionary *)dict {
    
    if (dict.allKeys.count==0) {
        return nil;
    }
    Class cls = [self class];
    id objcet = [cls new];
    [objcet setValuesForKeysWithDictionary:dict];
    return objcet;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    NSLog(@"%@", key);
}

//json数组直接转模型数组
+ (NSMutableArray *)dataArrayWithJsonArray:(NSArray *)array
{
    NSMutableArray *dataArr = [NSMutableArray new];
    for (NSDictionary *dict in array) {
        Class cls = [self class];
        id object = [cls modelWithDictionary:dict];
        [dataArr addObject:object];
    }
    return dataArr;
}
//获取当前类的类名
- (NSString *)className {
//    NSString *string = NSStringFromClass([self class]);
    return [[self class] className];
}

+ (NSString *)className {
    Class cls = [self class];
    return NSStringFromClass(cls);
}
//神拷贝一个对象
- (id)deepCopy {
    id obj = nil;
    @try {
        obj = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:self]];
    }
    @catch (NSException *exception) {
        NSLog(@"%@", exception);
    }
    return obj;
}


- (void)addControl
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIView *contol = [[UIControl alloc] initWithFrame:[UIScreen mainScreen].bounds];
    contol.backgroundColor = [UIColor blackColor];
    contol.alpha = 0.3;
    contol.tag = 1000;
    [window addSubview:contol];
//    [contol addTarget:self action:@selector(removeConrol) forControlEvents:UIControlEventTouchUpInside];
    
    //    UIActivity *actitvty = [[UIActivity alloc] init];
    
    UIView *view = [[UIView alloc] init];
    view.center = window.center;
    view.frame = CGRectMake(view.center.x-50, view.center.y-50, 100, 100);
    view.layer.cornerRadius = 10;
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.8;
    view.tag = 1001;
    [window addSubview:view];
    
    UIActivityIndicatorView *acV = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(10, 0, 80, 80)];
    acV.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    acV.center = CGPointMake(50, 40);
    //    acV.backgroundColor = [UIColor blackColor];
    acV.tag = 1002;
    acV.color = [UIColor grayColor];
    [acV startAnimating];
    
    [view addSubview:acV];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 70, 100, 20)];
    label.text = @"数据加载中";
    label.tag = 1003;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    [view addSubview:label];
    
    
    
}

- (void)removeConrol:(NSString *)statue
{
//    NSLog(@"jfdsdahfd");
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIView *view1 = [window viewWithTag:1000];
    UIView *view2 = [window viewWithTag:1001];
    UIActivityIndicatorView *acc = [window viewWithTag:1002];
    [acc stopAnimating];
    UILabel *label = [window viewWithTag:1003];
    label.text = statue;
    [UIView animateWithDuration:1 animations:^{
        view1.alpha = 0;
        view2.alpha = 0;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [view1 removeFromSuperview];
            [view2 removeFromSuperview];
            
        });
    }];
}





@end
